﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using UtfprLibrary.Windows.Forms.ComponentModel;

namespace UtfprLibrary.Windows.Forms {
    public partial class UtfprFormSplashBase : UtfprFormBase {
        private delegate void StringParameterDelegate(string Text);
        private delegate void StringParameterWithStatusDelegate(string Text, SplashMessageTypeEnum tom);
        private delegate void SplashShowCloseDelegate();
        
        //private void splashTimer_Tick(object sender, EventArgs e) {
        //    if (!InDesignMode) {
        //        if (UtfprApplication.Instance.IsLoginEnabled) {
        //            UtfprSecurity.Instance.ShowFormLoginDefault(false);
        //        }
        //    }
        //}
        
        /// <summary>
        /// To ensure splash screen is closed using the API and not by keyboard or any other things
        /// </summary>
        private bool CloseSplashScreenFlag = false;

        public UtfprFormSplashBase() {
            InitializeComponent();
        }

        public virtual Label GetStatusLabel() {
            throw new NotImplementedException("Sobreescreva este método2.");
        }

        public virtual ProgressBar GetProgressBar() {
            throw new NotImplementedException("Sobreescreva este método3.");
        }

        /// <summary>
        /// Displays the splashscreen
        /// </summary>
        internal void ShowSplashScreen() {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new SplashShowCloseDelegate(ShowSplashScreen));
                return;
            }
            this.Show();
            Application.Run(this);
        }

        /// <summary>
        /// Closes the SplashScreen
        /// </summary>
        internal void CloseSplashScreen() {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new SplashShowCloseDelegate(CloseSplashScreen));
                return;
            }
            CloseSplashScreenFlag = true;
            this.Close();
        }

        /// <summary>
        /// Update text with message color defined as green/yellow/red/ for success/warning/failure
        /// </summary>
        /// <param name="Text">Message</param>
        /// <param name="tom">Type of Message</param>
        internal void UdpateStatus(string Text, SplashMessageTypeEnum tom) {
            if (InvokeRequired) {
                // We're not in the UI thread, so we need to call BeginInvoke
                BeginInvoke(new StringParameterWithStatusDelegate(UdpateStatus), new object[] { Text, tom });
                return;
            }
            // Must be on the UI thread if we've got this far
            switch (tom) {
                case SplashMessageTypeEnum.Error:
                    GetStatusLabel().ForeColor = Color.Red;
                    break;
                case SplashMessageTypeEnum.Warning:
                    GetStatusLabel().ForeColor = Color.Goldenrod;
                    break;
                case SplashMessageTypeEnum.Success:
                    GetStatusLabel().ForeColor = Color.Green;
                    break;
                case SplashMessageTypeEnum.Info:
                    GetStatusLabel().ForeColor = SystemColors.Highlight;
                    break;
            }
            GetStatusLabel().Text = Text;
        }
        
        /// <summary>
        /// Prevents the closing of form other than by calling the CloseSplashScreen function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UtfprFormSplashBase_FormClosing(object sender, FormClosingEventArgs e) {
            if (CloseSplashScreenFlag == false) {
                e.Cancel = true;
            }
        }

        private void UtfprFormSplashBase_Load(object sender, EventArgs e) {
            if (!UtfprControlBase.IsInDesignMode) {
                GetProgressBar().Show();
            }
        }
    }
}
